package ac.svn.accesscontrol.controller;

import java.util.HashSet;
import java.util.Set;

import javax.swing.SwingUtilities;

import ac.notification.event.INotificationListener;
import ac.notification.event.NotificationEvent;
import ac.notification.manager.NotificationRegistrar;
import ac.svn.accesscontrol.store.IPriority;
import ac.svn.accesscontrol.store.IResources;
import ac.svn.accesscontrol.store.ISVNIdentifier;
import ac.svn.accesscontrol.store.ui.StatusItem;
import ac.svn.accesscontrol.utils.Utility;

public class SystemNotificationController implements INotificationListener,
ISVNIdentifier,IResources,IPriority {

	public SystemNotificationController()
	{
		//Register To Notification service.
		NotificationRegistrar.registerListener(this);
	}

	@Override
	public Set<String> declaredSubscriptionSet() {
		Set<String> subscriptions = new HashSet<String>();
		subscriptions.add(N_CONFIG_LOADED);
		subscriptions.add(N_CONFIG_MERGED);
		subscriptions.add(N_CONFIG_SAVED);
		subscriptions.add(N_CONFIG_SAVING_FAILED);
		subscriptions.add(N_SHOW_NOTIFICATION_MESSAGE);
		subscriptions.add(N_ACCESS_GENERATING_CONTENTS);
		subscriptions.add(N_ACCESS_FILE_SAVED);
		subscriptions.add(N_ACCESS_SAVING_FILE);
		subscriptions.add(N_ACCESS_FILE_SAVE_ABORTED);
		subscriptions.add(N_GENERAL_COMPLETION_MSG);
		subscriptions.add(N_GENERAL_ERROR_MSG);
		subscriptions.add(N_GENERAL_INFO_MSG);
		subscriptions.add(N_GENERAL_PROGRESS_MSG);
		subscriptions.add(N_GENERAL_WARNING_MSG);
		//	/subscriptions.add(N_CONFIG_SAVING_FAILED);
		return subscriptions;
	}

	@Override
	public void receiveEvent(final NotificationEvent notificationEvent) 
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			@Override
			public void run() 
			{
				handleNotifications(notificationEvent);
			}
		});
	}

	private void handleNotifications(NotificationEvent ne)
	{
		if(ne.getPropertyIdentifier().contains("CONFIG"))
		{
			sendConfigNotifications(ne);
		}
		else if(ne.getPropertyIdentifier().contains("ACCESS"))
		{
			sendAccessNotifications(ne);
		}
		else if(ne.getPropertyIdentifier().contains("GENERAL"))
		{
			sendCommandNotification(ne);
		}
	}

	private void sendCommandNotification(NotificationEvent ne) 
	{
		String noId 					= ne.getPropertyIdentifier();
		String message 				= (String) ne.getPropertyValue();
		StatusItem statusItem 	= new StatusItem(PRIORITY_NORMAL,message); 
		
		if(noId.contains(N_GENERAL_INFO_MSG))
		{
			statusItem.setIconURL(LOC_ICN_INFORMATION);
		}
		else if(noId.contains(N_GENERAL_COMPLETION_MSG))
		{
			statusItem.setIconURL(LOC_ICN_DONE);
		}
		else if(noId.contains(N_GENERAL_ERROR_MSG))
		{
			statusItem.setIconURL(LOC_ICN_ERROR);
		}
		else if(noId.contains(N_GENERAL_PROGRESS_MSG))
		{
			statusItem.setIconURL(LOC_ICN_PROCESSING);
		}
		else if(noId.contains(N_GENERAL_WARNING_MSG))
		{
			statusItem.setIconURL(LOC_ICN_WARNING_SMALL);
		}
		else if(noId.contains(N_GENERAL_COMPLETION_MSG))
		{
			statusItem.setIconURL(LOC_ICN_COMPLETE_SMALL);
		}
		
		Utility.sendNotification(CMD_SHOW_STATUS_MESSAGE,statusItem);
	}

	private void sendAccessNotifications(NotificationEvent ne) {
		String noId 					= ne.getPropertyIdentifier();
		String message 				= (String) ne.getPropertyValue();
		StatusItem statusItem 	= new StatusItem(PRIORITY_NORMAL,message); 

		if(noId.contains(N_ACCESS_FILE_SAVED))
		{
			statusItem.setIconURL(LOC_ICN_SAVE_SUCCESSFUL);
			statusItem.setPriority(PRIORITY_ABOVE_NORMAL);
			statusItem.setMessage("Access file saved successfully!");
			//statusItem.setStatusColor(Color.GREEN);
		}
		else if(noId.contains(N_ACCESS_SAVING_FILE))
		{
			statusItem.setIconURL(LOC_ICN_SAVING);
			statusItem.setPriority(PRIORITY_ABOVE_NORMAL);
			statusItem.setMessage("Saving access file..");
			//statusItem.setStatusColor(Color.YELLOW.brighter());
		} 
		else if(noId.contains(N_ACCESS_GENERATING_CONTENTS))
		{
			statusItem.setIconURL(LOC_ICN_PROCESSING);
			statusItem.setPriority(PRIORITY_ABOVE_NORMAL);
			statusItem.setMessage("Generating tags..");
			//statusItem.setStatusColor(Color.YELLOW.darker());
		}
		else if(noId.contains(N_ACCESS_FILE_SAVE_ABORTED))
		{
			statusItem.setIconURL(LOC_ICN_ERROR);
			statusItem.setPriority(PRIORITY_ABOVE_NORMAL);
			statusItem.setMessage("Saving operation cancelled!");
			//statusItem.setStatusColor(Color.YELLOW.darker());
		} 

		Utility.sendNotification(CMD_SHOW_STATUS_MESSAGE,statusItem);
	}

	private void sendConfigNotifications(NotificationEvent ne)
	{
		String noId 	= ne.getPropertyIdentifier();
		String message 	= (String) ne.getPropertyValue();
		StatusItem statusItem = new StatusItem(PRIORITY_NORMAL,message); 

		if(noId.contains(N_CONFIG_LOADED))
		{
			statusItem.setIconURL(LOC_ICN_LOAD_COMPLETE);
			statusItem.setPriority(PRIORITY_ABOVE_NORMAL);
			statusItem.setMessage("Configuration has been loaded successfully!");
		}
		else if(noId.contains(N_CONFIG_MERGED))
		{
			statusItem.setIconURL(LOC_ICN_MERGE);
			statusItem.setPriority(PRIORITY_ABOVE_NORMAL);
			statusItem.setMessage("Configuration has been merged successfully!");
		}
		else if(noId.contains(N_CONFIG_SAVED))
		{
			statusItem.setIconURL(LOC_ICN_SAVE_SUCCESSFUL);
			statusItem.setPriority(PRIORITY_ABOVE_NORMAL);
			statusItem.setMessage("Configuration has been saved successfully!");
		}
		else if(noId.contains(N_CONFIG_SAVING_FAILED))
		{
			statusItem.setIconURL(LOC_ICN_DONE);
			statusItem.setPriority(PRIORITY_MAX);  
			statusItem.setMessage("Save configuration operation failed!");
		}

		Utility.sendNotification(CMD_SHOW_STATUS_MESSAGE,statusItem);
	}
}
